package com.kob.backend.service.impl.ranklist;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kob.backend.mapper.UserMapper;
import com.kob.backend.pojo.Record;
import com.kob.backend.pojo.User;
import com.kob.backend.service.ranklist.GetRanklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author molimark<br />
 * @date: 2023/4/20 16:06<br/>
 * @description: <br/>
 */

@Service
public class GetRanklistServiceImpl implements GetRanklistService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public JSONObject getList(Integer page) {
        IPage<User> userIPage = new Page<>(page,10);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("rating");
        List<User> users = userMapper.selectPage(userIPage,queryWrapper).getRecords();
        JSONObject resp = new JSONObject();
        // 传给前端的user有密码，不安全，所以先将密码清空
        List<JSONObject> items = new LinkedList<>();
        int index=0;
        for(User user:users){
            JSONObject item = new JSONObject();
            user.setPassword("");
            // 当前玩家的排名,前端实现不太会，v-for里用current_page的话一直都是1
            item.put("index",(page-1)*3+(++index));
            item.put("user",user);
            items.add(item);
        }
        resp.put("users",items);
        resp.put("users_cnt",userMapper.selectCount(null));
        return resp;
    }
}
